package io.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
 * 演示字节流:
 * 		输入：InputStream
 * 		输出：OutputStream
 * 文件字节输入输出流
 * 		输入：FileInputStream
 * 		输出：FileOutputStream
 * 使用字节输入流的范式：
 * 		1.读一次
 * 		2.判断是否结尾
 * 		3.循环读
 * @author Administrator
 *
 */
public class ByteIODemo {

	/**
	 * 演示文件字节输入流
	 * @throws IOException 
	 */
	@Test
	public void demo() throws IOException {
		
		FileInputStream input = new FileInputStream("/Users/wangzhipeng/Desktop/a.txt");
		
		// read()，读取到返回的就是内容
//		int content = input.read();
//		while(content != -1) {
//			System.out.print((char)content);  //中文乱码
//			content = input.read();
//		}
		
		/*
		 * read(byte[]),读取到的内容存储到byte数组中，返回的结果是读取到的长度
		 * read(byte[])数组的形式在一定程度上可以解决中文乱码，但是这个不靠谱，在分界线的时候会将汉字拆开
		 * */ 
		byte[] b = new byte[1024];
		int length = input.read(b);
		while(length != -1) {
			System.out.println(new String(b));
			length = input.read(b);
		}
		
//		input.read(b, off, len); b:byte数组	off:偏移量	len:长度
		
		input.close();
	}
	
	/**
	 * read(byte[])：不清空的问题
	 * @throws Exception 
	 */
	@Test
	public void demo2() throws Exception {
		FileInputStream inputStream = new FileInputStream("/Users/wangzhipeng/Desktop/a.txt");
		
		byte[] b = new byte[10];
		int length = inputStream.read(b);
		while(length != -1) {
//			System.out.println(new String(b));
			System.out.print(new String(b, 0, length));
			length = inputStream.read(b);
		}
		
		inputStream.close();
	}
	
	/**
	 *文件字节输出流 
	 * @throws IOException 
	 */
	@Test
	public void demo3() throws IOException {
		FileOutputStream fileOutputStream = new FileOutputStream("/Users/wangzhipeng/Desktop/a.txt");

		// 写int形式
//		fileOutputStream.write(65); 
		
		// 写byte数组
		String content = "今天天气不错\nhello";
		fileOutputStream.write(content.getBytes());
		
		// 写带有偏移量的byte数组
//		fileOutputStream.write(b, off, len); // 最常用
		
		fileOutputStream.close();
	}
	
	/**
	 * 实现复制文件的案例
	 * 1.输入流读取
	 * 2.输出流写出去
	 * 3.关闭流
	 * @throws IOException 
	 */
	@Test
	public void demo4() throws IOException {
		// 1.创建流
		InputStream in = new FileInputStream("/Users/wangzhipeng/Desktop/a.txt");
		OutputStream out = new FileOutputStream("/Users/wangzhipeng/Desktop/b.txt");
		
		long timeStart = System.currentTimeMillis();
		// 2.复制
		int content = in.read();
		while(content != -1) {
			// 写到文件
			out.write(content);
			// 继续读
			content = in.read();
		}
		long timeEnd = System.currentTimeMillis();
		System.out.println("复制花费了：" + (timeEnd-timeStart));
		
		// 3.关闭资源
		in.close();
		out.close();
	}
	
	/**
	 * 使用byte数组的形式
	 * @throws IOException 
	 */
	@Test
	public void demo5() throws IOException {
		// 1.创建流
		InputStream in = new FileInputStream("G:\\a.txt");
		OutputStream out = new FileOutputStream("F:\\c.txt");
		
		long timeStart = System.currentTimeMillis();
		// 2.复制
		byte[] b = new byte[1024];
		int length = in.read(b);
		while(length != -1) {
			// 写文件
			out.write(b, 0, length);
			// 继续读取
			length = in.read(b);
		}
		long timeEnd = System.currentTimeMillis();
		System.out.println("复制花费了：" + (timeEnd-timeStart));
		
		// 3.关闭流
		in.close();
		out.close();

	}
	
}
